home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / ip / sized_io.shar / writer.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-29  |  1.2 KB  |  62 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/time.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. /* #include <netdb.h> */
  7. #include "inet.h"
  8.  
  9. char msg[2000];
  10.  
  11. struct timeval timeout = {0L, 0L};
  12.  
  13. main(argc,argv)
  14. int argc;
  15. char **argv;
  16. {
  17.     extern char *optarg;
  18.     int c, debugLevel = 0;
  19.     char *host = 0;
  20.  
  21.     int cc;
  22.     int i;
  23.     int fd;
  24.     fd_set readfds;
  25.     int maxfds;
  26.  
  27.     FD_ZERO(&readfds);
  28.  
  29.     while ((c = getopt(argc,argv,"h:")) != EOF) {
  30.         switch (c) {
  31.         case 'h': host = optarg; break;
  32.         default: exit(-1);
  33.         }
  34.     }
  35.  
  36.     maxfds = getdtablesize();
  37.         /* prevent narrow fd_set binaries from blowing up with with */
  38.     /* large getdtablesize() */
  39. #ifdef FD_SET_SIZE
  40.     maxfds = ((FD_SET_SIZE > maxfds)?maxfds:FD_SET_SIZE);
  41. #endif
  42.  
  43.     fd = initport(PORT_NUMBER(2000),CLIENT,SOCK_STREAM,host);
  44.  
  45.     if (fd < 0) {
  46.         fprintf(stderr,"initport() = %d\n",fd);
  47.         exit(-1);
  48.     }
  49.     for (i=0;;i++) {
  50.         printf("%d: ",i);
  51.         if (0 == gets(msg)) exit(0); /* exit on EOF */
  52.         cc = sized_write(fd,msg,strlen(msg)+1);
  53.         FD_SET(fd,&readfds);
  54.         if (0 < select(maxfds,&readfds,0,0,&timeout)
  55.            && FD_ISSET(fd,&readfds)) {
  56.             cc = sized_read(fd,msg,2000);
  57.             msg[cc] = '\0';
  58.             printf("msg from server: %s\n",msg);
  59.         }
  60.     }
  61. }
  62.